/**
 * JAVACC DEMO 1.0
 */
package com.apache.info.template;

import com.apache.api.vo.ParamsVo;
import com.apache.database.constant.SpringContextLoader;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.PubMetadataManager;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description: 生成sql
 *
 * @author dyhou 创建时间：2016-9-29
 */
public class CreateTableSql {

    PubSortInfoManager sortInfoManager;// = LoadManagerFactory.getInstance().getManager("systemModelManager");

    PubMetadataManager metadataManager;

    private static CreateTableSql instance;

    private CreateTableSql() {
        sortInfoManager = (PubSortInfoManager) SpringContextLoader.getBean("pubSortInfoManager");
        metadataManager = (PubMetadataManager) SpringContextLoader.getBean("pubMetadataManager");
    }

    public static CreateTableSql instance() {
        if (null == instance) {
            instance = new CreateTableSql();
        }
        return instance;
    }

    /**
     * description:  生成数据库创建脚本
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public String createTable(Map<String, String> params) {
        String ids = params.get("ids");
        String[] str = ids.split(",");
        String databaseType = StrUtil.doNull(params.get("databaseType"), "oracle");
        StringBuffer sb = new StringBuffer();
        Map param = new HashMap();
        PubMetadata data = new PubMetadata();
        int count = str.length;
        ParamsVo vo = new ParamsVo();
        for (int i = 0; i < count; i++) {
            if (ToolsUtil.isNull(str[i]))
                continue;
            vo.setInfoId(str[i]);
            PubSortInfo obj = (PubSortInfo) sortInfoManager.getInfoById(vo);
            data.setSortId(str[i]);
            vo.setObj(data);
            List<PubMetadata> list = metadataManager.getList(vo);
            vo.setObj(null);
            String sql = "";
            if ("oracle".equalsIgnoreCase(databaseType)) {
                sql = getTableOracleSql(obj, param, list);
            } else if ("mySql".equalsIgnoreCase(databaseType)) {
                sql = getTableMysqlSql(obj, param, list);
            } else if ("sqlserver".equalsIgnoreCase(databaseType)) {
                sql = getTableSqlServerSql(obj, param, list);
            }
            sb.append(sql);
        }
        return sb.toString();
    }

    private String getTableOracleSql(PubSortInfo info, Map param, List<PubMetadata> list) {
        StringBuffer sb = new StringBuffer();
        if (ToolsUtil.isEmpty(list)) {
            return "";
        }
        String pk = "";
        sb.append("create table " + String.valueOf(info.getInfoTableName()) + "( <br>");
        for (PubMetadata mp : list) {
            sb.append(String.valueOf(mp.getDataShortName()) + " ");
            String dataType = mp.getDataType();
            String dataLength = String.valueOf(mp.getDataLength());
            String dataRestrict = mp.getDataRestrict();
            if ("1".equals(dataType) || "2".equals(dataType)) {
                sb.append(" number(" + dataLength + ") ");
            } else if ("3".equals(dataType)) {
                sb.append(" number(" + dataLength + ",6) ");
            } else if ("4".equals(dataType)) {
                sb.append(" char(" + dataLength + ") ");
            } else if ("6".equals(dataType)) {
                sb.append(" date ");
            } else if ("7".equals(dataType)) {
                sb.append(" clob ");
            } else {
                sb.append(" varchar2(" + dataLength + ") ");
            }
            if ("1".equals(dataRestrict) || "2".equals(dataRestrict)) {
                sb.append(" not null ");
                if ("1".equals(dataRestrict)) {
                    pk = mp.getDataShortName();
                }
            }
            sb.append(", <br>");
        }
        if (ToolsUtil.isNotNull(pk))
            sb.append(" primary key(" + pk + ") <br>");
        sb.append("); <br>");
        return sb.toString();
    }

    private String getTableMysqlSql(PubSortInfo info, Map param, List<PubMetadata> list) {
        StringBuffer sql = new StringBuffer();
        if (ToolsUtil.isEmpty(list)) {
            return "";
        }
        String pk = "";
        sql.append("create table " + String.valueOf(info.getInfoTableName()) + "( <br>");
        for (PubMetadata mp : list) {
            String dataType = mp.getDataType();
            int dataLength = Integer
                    .valueOf(StrUtil.doNull(String.valueOf(mp.getDataLength()), "30"));
            String dataRestrict = mp.getDataRestrict();
            sql.append(" " + mp.getDataShortName() + " ");
            if ("1".equals(dataType)) {
                sql.append(" int ");
            } else if ("2".equals(dataType)) {
                sql.append(" bigint ");
            } else if ("3".equals(dataType)) {
                sql.append(" float");
            } else if ("4".equals(dataType)) {
                sql.append(" char(" + dataLength + ") ");
            } else if ("6".equals(dataType)) {
                sql.append(" datetime ");
            } else if ("7".equals(dataType)) {
                sql.append(" longtext ");
            } else if ("8".equals(dataType)) {
                sql.append(" decimal(" + dataLength + ") ");
            }  else {
                sql.append(" varchar(" + dataLength + ") ");
            }
            if ("1".equals(dataRestrict) || "2".equals(dataRestrict)) {
                sql.append(" not null ");
                if ("1".equals(dataRestrict)) {
                    pk = mp.getDataShortName();
                }
            }
            sql.append(", <br>");
        }
        if (ToolsUtil.isNotNull(pk))
            sql.append(" primary key(" + pk + ") <br>");
        sql.append("); <br>");
        return sql.toString();
    }

    private String getTableSqlServerSql(PubSortInfo info, Map param, List<PubMetadata> list) {
        StringBuffer sql = new StringBuffer();
        if (ToolsUtil.isEmpty(list)) {
            return "";
        }
        String pk = "";
        sql.append("create table " + String.valueOf(info.getInfoTableName()) + "( <br>");
        for (PubMetadata mp : list) {
            String dataType = mp.getDataType();
            int dataLength = Integer
                    .valueOf(StrUtil.doNull(String.valueOf(mp.getDataLength()), "30"));
            String dataRestrict = mp.getDataRestrict();
            sql.append(" " + mp.getDataShortName() + " ");
            if ("1".equals(dataType)) {
                sql.append(" int ");
            } else if ("2".equals(dataType)) {
                sql.append(" bigint ");
            } else if ("3".equals(dataType)) {
                sql.append(" float");
            } else if ("4".equals(dataType)) {
                sql.append(" char(" + dataLength + ") ");
            } else if ("6".equals(dataType)) {
                sql.append(" datetime ");
            } else if ("7".equals(dataType)) {
                sql.append(" text ");
            } else {
                sql.append(" varchar(" + dataLength + ") ");
            }
            if ("1".equals(dataRestrict) || "2".equals(dataRestrict)) {
                sql.append(" not null ");
                if ("1".equals(dataRestrict)) {
                    pk = String.valueOf(mp.getDataShortName());
                }
            }
            sql.append(", <br>");
        }
        if (ToolsUtil.isNotNull(pk))
            sql.append(" primary key(" + pk + ") <br>");
        sql.append("); <br>");
        return sql.toString();
    }
}
